package io.finer.erp.jeecg.sale.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.finer.erp.jeecg.approve.service.impl.ApproveServiceImpl;
import io.finer.erp.jeecg.sale.entity.SaleOrder;
import io.finer.erp.jeecg.sale.entity.SaleOrderLine;
import io.finer.erp.jeecg.sale.mapper.SaleOrderLineMapper;
import io.finer.erp.jeecg.sale.mapper.SaleOrderMapper;
import io.finer.erp.jeecg.sale.service.SaleOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 销售订单 服务实现类
 * </p>
 *
 * @author yaoyifan
 * @since 2021-05-11
 */
@Service
public class SaleOrderServiceImpl extends ApproveServiceImpl<SaleOrderMapper, SaleOrder> implements SaleOrderService {
    @Autowired
    SaleOrderLineMapper saleOrderLineMapper;

    @Override
    @Transactional
    public void saveMain(SaleOrder saleOrder, List<SaleOrderLine> saleOrderLineList) {
        this.baseMapper.insert(saleOrder);
        if(saleOrderLineList != null && saleOrderLineList.size() > 0){
            for(SaleOrderLine entity: saleOrderLineList){
                entity.setMid(saleOrder.getId());
                saleOrderLineMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void updateMain(SaleOrder saleOrder, List<SaleOrderLine> saleOrderLineList) {
        this.baseMapper.updateById(saleOrder);
        QueryWrapper<SaleOrderLine> queryWrapper = new QueryWrapper<>();
        saleOrderLineMapper.delete(queryWrapper.eq("mid",saleOrder.getId()));
        if (saleOrderLineList != null && saleOrderLineList.size() > 0){
            for(SaleOrderLine entity: saleOrderLineList){
                entity.setMid(saleOrder.getId());
                saleOrderLineMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        QueryWrapper<SaleOrderLine> queryWrapper = new QueryWrapper<>();
        saleOrderLineMapper.delete(queryWrapper.eq("mid",id));
        this.baseMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            QueryWrapper<SaleOrderLine> queryWrapper = new QueryWrapper<>();
            saleOrderLineMapper.delete(queryWrapper.eq("mid",id.toString()));
            this.baseMapper.deleteById(id.toString());
        }
    }

    @Override
    @Transactional
    public void submit(String model, String id) {
        super.submit(model, id, this);
        SaleOrder saleOrder = new SaleOrder();
        saleOrder.setId(id);
        saleOrder.setBillProcStatus("submit");
        this.baseMapper.updateById(saleOrder);
    }

    @Override
    @Transactional
    public void reject(String model, String id, String note) {
        super.reject(model, id, note);
        SaleOrder saleOrder = new SaleOrder();
        saleOrder.setId(id);
        saleOrder.setBillProcStatus("reject");
        this.baseMapper.updateById(saleOrder);
    }

    @Override
    @Transactional
    public String approve(String model, String id, String note) {
        String approveInstanceStatus = super.approve(model, id, note);
        SaleOrder saleOrder = new SaleOrder();
        saleOrder.setId(id);
        saleOrder.setBillProcStatus(approveInstanceStatus);
        this.baseMapper.updateById(saleOrder);
        return approveInstanceStatus;
    }

    @Override
    @Transactional
    public void withDraw(String model, String id) {
        super.withDraw(model, id);
        SaleOrder saleOrder = new SaleOrder();
        saleOrder.setId(id);
        saleOrder.setBillProcStatus("create");
        this.baseMapper.updateById(saleOrder);
    }

    @Transactional
    public void invalid(String id, String note) {
        SaleOrder saleOrder = new SaleOrder();
        saleOrder.setId(id);
        saleOrder.setBillProcStatus("invalid");
        saleOrder.setInvalidationReason(note);
        saleOrder.setIsVoided(1);
        this.baseMapper.updateById(saleOrder);
    }
}
